package org.zbclan.bf2.rcon;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

public class ShellCommand {

	private String[] envp;
	private File dir;

	public ShellCommand() {
	}

	public ShellCommand(File dir) {
		this.dir = dir;
	}

	public ShellCommand(String... envp) {
		this.envp = envp;
	}

	public ShellCommand(File dir, String... envp) {
		this.dir = dir;
		this.envp = envp;
	}

	public String execute(String... command) throws IOException {

		StringWriter sw = new StringWriter();

		Process process = Runtime.getRuntime().exec(command, envp, dir);
		InputStream is = process.getInputStream();
		int c = is.read();
		while (c != -1) {
			sw.write(c);
			c = is.read();
		}

		return sw.toString();
	}

	public String[] getEnvp() {
		return envp;
	}

	public void setEnvp(String[] envp) {
		this.envp = envp;
	}

	public File getDir() {
		return dir;
	}

	public void setDir(File dir) {
		this.dir = dir;
	}
}
